import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

class VideoApp extends StatefulWidget {     //必须写在有状态组件内
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<VideoApp> {
  VideoPlayerController _controller;      //先定义一个VideoPlayer的控制器变量
  @override
  void initState() {    //重写组件初始化方法
    // TODO: implement initState
    super.initState();
    _controller=VideoPlayerController.network(//定义连接器内容，这里初学者可能有点难懂下面详细讲
        "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
    )..initialize().then((a){
      setState(() {

      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('视频测试'),
      ),
      body: Center(
          child: _controller.value.initialized ? AspectRatio(   //     这里和上面是联动的，下面详细讲
            aspectRatio: _controller.value.aspectRatio,
            child: VideoPlayer(_controller),
          ):Container(
            width: 100,
            height: 100,
            color: Colors.blue,
          )
      ),
      floatingActionButton: FloatingActionButton(   //随便定义一个按钮用于测试
        onPressed: () {
          setState(() {
            _controller.value.isPlaying   //设置播放状态
                ? _controller.pause()
                : _controller.play();
          });
        },
        child: Icon(
          _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
        ),
      ),
    );
  }
}